//
//  ControlAnnotationView.m
//  PlayingWithMaps
//
//  Created by Jaime on 6/25/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "ControlAnnotationView.h"
#import "ControlAnnotation.h"
#include "UIUtils.h"


@interface ControlAnnotationView()

// forward declarations
-(void) resetDragAndDrop;

@end



@implementation ControlAnnotationView

@synthesize mapView = _mapView;

- (id)initWithAnnotation:(id <MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier {
	NSLog(@"init with annotations");
	if((self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier])) {
		self.canShowCallout = YES;
		self.multipleTouchEnabled =NO;
		_mapView = nil;
		
		UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
		[rightButton addTarget:self action:@selector(showAnnotationAddress) forControlEvents:UIControlEventTouchUpInside];
		self.rightCalloutAccessoryView = rightButton;
	}
	
	
	return self;
}

- (id)initWithFrame:(CGRect)frame {
    if ((self = [super initWithFrame:frame])) {
        // Initialization code
    }
    return self;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

#pragma mark -
#pragma mark User interaction

- (void) showAnnotationAddress:(id)sender {
	[UIUtils showAlert:@"this is the address"];
}

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
	NSLog(@"touches started");
	UITouch* touch = [touches anyObject];
	_startLocation = [touch locationInView:[self superview]];
	_originalCenter = self.center;
	
	[super touchesBegan:touches withEvent:event];
	
}

- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
	UITouch* touch = [touches anyObject];
	CGPoint newLocation = [touch locationInView:[self superview]];
	CGPoint newCenter;
	
	//if the user moved more that 5px begin the drag
	if( (abs(newLocation.x - _startLocation.x) > 5.0 ) ||
	   (abs(newLocation.y - _startLocation.y) > 5.0 ))
		_isMoving = YES;
	
	if( _isMoving)
	{
		newCenter.x = _originalCenter.x + (newLocation.x - _startLocation.x);
		newCenter.y = _originalCenter.y + (newLocation.y - _startLocation.y);
		self.center = newCenter;
	}
	else
	{
		[super touchesMoved:touches withEvent:event];
	}
}

- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
	if( _isMoving)
	{
		// update the map coordinates on the annotation
		CGPoint newCenter = self.center;
		ControlAnnotation* an = self.annotation;
		CLLocationCoordinate2D newCoord = [_mapView convertPoint:newCenter toCoordinateFromView:self.superview];
		
		[an changeCoordinate:newCoord];
		
		[self resetDragAndDrop];
	}
}

- (void) touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
	if(_isMoving)
	{
		self.center = _originalCenter;
		[self resetDragAndDrop];
		
	}
}

-(void) resetDragAndDrop {
	_startLocation = CGPointZero;
	_originalCenter = CGPointZero;
	_isMoving = NO;
}

#pragma mark -
#pragma mark memory management

- (void)dealloc {
    [super dealloc];
}


@end
